﻿using System;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.IO;
using System.Reflection;
using System.Text;
using DTO;
using System.Web;

public class Pagina : System.Web.UI.Page
{
    public Permissao Usuario
    {
        get { return Session["usuario"] != null ? (Permissao)Session["usuario"] : Permissao.None; }
    }

    public int TipoParam
    {
        get { return Request.QueryString["tipoe"] != null ? Convert.ToInt32(Request.QueryString["tipoe"]) : -1; }
    }
    
    public string [] TipoDetalhadoParam
    {
        get { return Request.QueryString["tipod"] != null ? Request.QueryString["tipod"].Split(',') : null; }
    }

    public string QueryParam
    {
        get { return Request.QueryString["query"] != null ? HttpUtility.UrlDecode(Request.QueryString["query"]) : string.Empty; }
    }

    public Pagina()
    {



    }

    public void MontaMapasPesquisa(GridView grid)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("jQuery(document).ready(function(){");
        for (int i = 0; i < grid.Rows.Count; i++)
        {
            if (grid.Rows[i].RowType == DataControlRowType.DataRow)
            {
                string nomeLocal = ((Label)grid.Rows[i].FindControl("lblNome")).Text;
                string endereço = ((Label)grid.Rows[i].FindControl("lblEndereço")).Text;
                string link = grid.Rows[i].FindControl("lnkMapa").ClientID;
                string div = grid.Rows[i].FindControl("map_canvas").ClientID;

                sb.AppendLine("jQuery('#" + link + "').mouseover(function(){ jQuery('#" + div + "').gMap({ markers: [{ address: '" + endereço + "', html: '<span><strong>" + nomeLocal + "</strong></span>' }], address: '" + endereço + "', zoom: 16 }); ShowContent('" + div + "'); return true; });");
                sb.AppendLine("jQuery('#" + link + "').mouseout(function(){ HideContent('" + div + "'); return true; });");
            }
        }
        sb.AppendLine("});");
        sb.AppendLine("");

        ClientScript.RegisterStartupScript(this.GetType(), "mapsScript", sb.ToString(), true);

    }

    public void Alert(string message)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
    }



    protected void RowDataBound(GridView grid, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int id = Convert.ToInt32(grid.DataKeys[e.Row.RowIndex].Value);
                string nomeLocal = ((Label)e.Row.FindControl("lblNome")).Text;
                string endereço = ((Label)e.Row.FindControl("lblEndereço")).Text;

                ((HyperLink)e.Row.FindControl("lnkImgDetalhar")).NavigateUrl = "detalhar.aspx?id=" + id;
                ((HyperLink)e.Row.FindControl("lnkMapa")).NavigateUrl = "detalhar.aspx?id=" + id + "#mapa";
                ((Image)e.Row.FindControl("imgThumbnailumbnail")).ImageUrl = CaminhoImagemThumbnail(id).Replace("\\", "/");
                ((Label)e.Row.FindControl("lblNome")).Text = nomeLocal.Length > 40 ? nomeLocal.Remove(40) + "..." : nomeLocal;

                if (Usuario == Permissao.Admin)
                    ((HyperLink)e.Row.FindControl("lnkEditar")).NavigateUrl = "editar.aspx?id=" + id;
                else
                    ((HtmlControl)e.Row.FindControl("div_lnkEditar")).Visible = false;
            }

        }
        catch (Exception ex)
        {
            Log.Write(ex, "Pagina.cs -> " + MethodInfo.GetCurrentMethod().Name);
        }
    }
    public string CaminhoImagemThumbnail(int id)
    {
        string path = @"images\equipamentos\" + id + "th.png";
        if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + path))
        {
            return path;
        }
        else
            return @"images\noimg_transp.png";
    }
    public string CaminhoImagem(int id)
    {
        string path = @"images\equipamentos\" + id + ".png";
        if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + path))
        {
            return path;
        }
        else
            return @"images\noimg_transp.png";
    }
    public bool ExisteImagem(int id)
    {
        string path = @"images\equipamentos\" + id + "th.png";
        return File.Exists(AppDomain.CurrentDomain.BaseDirectory + path);
    }
}